package com.iafme.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.iafme.dto.req.ShowingsReqDTO;
import com.iafme.dto.res.ShowingsResDTO;
import com.iafme.model.Showings;
import com.iafme.service.IShowingsService;
import com.systemframework.annotation.OperateAnnotation;
import com.systemframework.common.BaseCommonInterface;
import com.systemframework.enums.BusinessType;
import com.systemframework.util.Result;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author summer
 * @version 1.0
 * @date 2025/1/7 18:03
 */
@RestController
@RequestMapping(value = "/showings")
@AllArgsConstructor
public class ShowingsController implements BaseCommonInterface<ShowingsReqDTO, ShowingsResDTO> {

    private final IShowingsService showingsService;

    @OperateAnnotation(businessType = BusinessType.INSERT,name = "报考场次")
    @SaCheckPermission("enter:showings:add")
    @Override
    public Result<String> save(@Valid ShowingsReqDTO showingsReqDTO) {
        Showings showings = Convert.convert(Showings.class, showingsReqDTO);
        showingsService.save(showings);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.UPDATE,name = "报考场次")
    @SaCheckPermission("enter:showings:edit")
    @Override
    public Result<String> update(@Valid ShowingsReqDTO showingsReqDTO) {
        Showings showings = Convert.convert(Showings.class, showingsReqDTO);
        showingsService.updateById(showings);
        return Result.successResult();
    }

    @OperateAnnotation(businessType = BusinessType.DELETE,name = "报考场次")
    @SaCheckPermission("enter:showings:delete")
    @Override
    public Result<String> del(Integer id) {
        showingsService.removeById(id);
        return Result.successResult();
    }

    @Override
    public Result<Page<ShowingsResDTO>> lists(ShowingsReqDTO showingsReqDTO) {
        return Result.successResult(showingsService.lists(showingsReqDTO));
    }

    @Override
    public Result<ShowingsResDTO> findById(Integer id) {
        Showings showings = showingsService.getById(id);
        return Result.successResult(Convert.convert(ShowingsResDTO.class,showings));
    }

    @OperateAnnotation(businessType = BusinessType.BATCHDELETE,name = "报考场次")
    @SaCheckPermission("enter:showings:delete")
    @Override
    public Result<String> batchDel(List<ShowingsResDTO> list) {
        List<Integer> ids = list.stream().map(ShowingsResDTO::getId).collect(Collectors.toList());
        showingsService.removeByIds(ids);
        return Result.successResult();
    }

    @GetMapping(value = "/findAll/{levelId}")
    public  List<ShowingsResDTO> findAll(@PathVariable Integer levelId) {
        LambdaQueryWrapper<Showings> eq = Wrappers.lambdaQuery(Showings.class).eq(Showings::getLevelId, levelId);
        List<Showings> list = showingsService.list(eq);
        return Convert.toList(ShowingsResDTO.class,list);
    }
}
